import { InlineConfig, build } from 'vite';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import dts from 'vite-plugin-dts';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const entries: Array<{
  entry: string;
  name: string;
}> = [
  { entry: '../src/index.ts', name: 'index' },
  { entry: '../src/love.ts', name: 'love' },
  { entry: '../src/words.ts', name: 'words' },
  { entry: '../src/fireworks.ts', name: 'fireworks' },
  { entry: '../src/cursor.ts', name: 'cursor' }
];

// 动态生成配置build配置选项
const profiles = entries.flatMap(({ entry, name }): InlineConfig => {
  return {
    build: {
      // sourcemap: true,
      emptyOutDir: false,
      minify: 'esbuild',
      lib: {
        name: name === 'index' ? 'clicker' : name,
        formats: ['umd', 'es'],
        entry: resolve(__dirname, entry),
        fileName: format => `${name}.${format === 'umd' ? 'umd.js' : 'js'}`
      }
    },
    plugins: [
      dts({
        entryRoot: 'src',
        copyDtsFiles: false,
        insertTypesEntry: true
      })
    ]
  };
});

// 入口
async function main() {
  // 遍历配置选项，手动调用build方法
  for (const profile of profiles) {
    await build(profile);
  }
}

main();
